package com.icarus.entity;

import com.icarus.view.MainView;

import javax.swing.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 角色飞机
 */
public class Hero extends Plane {

    //初始化角色
    public Hero() {
        //角色图标
        this.setIcon(new ImageIcon("images/hero/hero.png"));
        //我方角色飞机是否活着
        this.setAlive(true);
        this.setHp(1);  //1点生命值
        //角色速度
        this.setSpeed(20);
        //设置弹药库
        List<Bullet> bullets = Collections.synchronizedList(new ArrayList<>());
        this.setBullets(bullets);

        //初始化角色坐标
        //初始化英雄角色位置和速度
        this.setX(MainView.WIDTH/2 - 50);
        this.setY(MainView.HEIGHT/2+100);
    }

    //向上移动
    public void moveUp() {
        //不能飞过边界外
        this.setDirection(0);
        if(this.getY()>(0+this.getSpeed())&&!isTouch()) {
            this.setY(this.getY()-this.getSpeed());
        }

    }

    //向下移动
    public void moveDown() {
        this.setDirection(1);
        //不能飞过边界外
        if(this.getY()<(MainView.HEIGHT-this.getIcon().getIconHeight()-this.getSpeed())&&!isTouch()) {
            this.setY(this.getY()+this.getSpeed());
        }

    }

    //向左移动
    public void moveLeft() {
        //不能飞过边界外
        this.setDirection(2);
        if(this.getX()>(0+this.getSpeed())&&!isTouch()) {
            this.setX(this.getX()-this.getSpeed());
        }

    }

    //向右移动
    public void moveRight() {
        //不能飞过边界外
        this.setDirection(3);
        if(this.getX()<(MainView.WIDTH-this.getIcon().getIconWidth()-this.getSpeed())&&!isTouch()) {
            this.setX(this.getX()+this.getSpeed());
        }

    }



    //
    public boolean isTouch() {

        //获取英雄的坐标
        int x = this.getX();
        int y = this.getY();

        //获取英雄的宽度和长度
        int width = this.getIcon().getIconWidth();
        int height = this.getIcon().getIconHeight();


        List<Enemy> enemys = MainView.enemys;

        //获取所有的敌人

        for(Enemy enemy:enemys) {
            int enemyX = enemy.getX();
            int enemyY = enemy.getY();

            int enemyWidth = enemy.getIcon().getIconWidth();
            int enemyHeight = enemy.getIcon().getIconHeight();



            switch (this.getDirection()) {
                case 0:
                    //向上

                    if((y>=enemyY&&y<=enemyY+enemyHeight)&&((x>=enemyX&&x<=enemyX+enemyWidth)||(x+width>=enemyX&&x+width<=enemyX+enemyWidth))) {

                        return true;
                    }

                    break;
                case 1:
                    //向下
                    if((y+height>=enemyY&&y+height<=enemyY+enemyHeight)&&((x>=enemyX&&x<=enemyX+enemyWidth)||(x+width>=enemyX&&x+width<=enemyX+enemyWidth))) {


                        return true;
                    }

                    break;
                case 2:
                    //向左

                    if((x>=enemyX&&x<=enemyX+enemyWidth)&&((y>=enemyY&&y<=enemyY+enemyHeight)||(y+height>=enemyY&&y+height<=enemyY+enemyHeight))) {
                        return true;
                    }

                    break;
                case 3:

                    //向右
                    if((x+width>=enemyX&&x+width<=enemyX+enemyWidth)&&((y>=enemyY&&y<=enemyY+enemyHeight)||(y+height>=enemyY&&y+height<=enemyY+enemyHeight))) {

                        return true;
                    }

                    break;
            }



        }


        return false;
    }

    //攻击
    @Override
    public void attack() {

        //给英雄添加子弹
        Bullet bullet = new Bullet();
        //英雄的的子弹向上飞
        bullet.setDirection(0);
        //子弹图标
        bullet.setIcon(new ImageIcon("images/bullet/7.png"));
        //子弹位置英雄位置正中间
        bullet.setX(this.getX()+this.getIcon().getIconWidth()/2-bullet.getIcon().getIconWidth()/2);
        bullet.setY(this.getY()-bullet.getIcon().getIconHeight());
        //子弹速度
        bullet.setSpeed(30);
        //子弹状态
        bullet.setStatus(0);
        //开启子弹线程让子弹飞
        Thread thread = new Thread(bullet);
        thread.start();

        //往弹药库添加子弹
        this.getBullets().add(bullet);

  }

}
